import logger from '../core/logger';
import apiInfo from './core/apiInfo';
import apiProvider from './core/apiProvider';
import { clientBase } from './core/clientBase';
import returnModel from '../model/common/returnModel';
import userFollowItemModel from '../model/user/userFollowItemModel';

class followClient  extends clientBase {
  constructor() {
    super();
  }

  getFollowings(mid: number, page: number): Promise<returnModel<Array<userFollowItemModel>>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let url = 'https://api.bilibili.com/x/relation/followings';
        let condition = `vmid=${mid}&ps=30&pn=${page}&order=desc&order_type=attention`;
        apiProvider.getWbiSign(condition).then(parameter => {
          let info = new apiInfo(url, parameter);
          this.client.getResult(info).then((res) => {
            if (res.code == 0) {
              var data = res.data.list as Array<userFollowItemModel>;
              resolve(self.buildSuccessResult<Array<userFollowItemModel>>(data));
            } else
              resolve(self.buildFailedResult<Array<userFollowItemModel>>(res.message));
          });
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<Array<userFollowItemModel>>(e));
      }
    });
  }

  getFollowers(mid: number, page: number): Promise<returnModel<Array<userFollowItemModel>>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.bilibili.com/x/relation/followers", `vmid=${mid}&ps=30&pn=${page}&order=desc`);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            var data = res.data.list as Array<userFollowItemModel>;
            resolve(self.buildSuccessResult<Array<userFollowItemModel>>(data));
          } else
            resolve(self.buildFailedResult<Array<userFollowItemModel>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<Array<userFollowItemModel>>(e));
      }
    });
  }
}
export default new followClient();